package com.cn.topAndFoot.service.impl;
import com.cn.common.pojo.CompanyModel;
import com.cn.common.pojo.ResultModel;
import com.cn.topAndFoot.service.EsService;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static org.elasticsearch.index.query.QueryBuilders.matchQuery;

@Service
public class EsServiceImpl implements EsService {
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Override
    public ResultModel esFindAll(CompanyModel companyModel) {
        NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
                .withPageable(PageRequest.of(companyModel.getPageNum()-1,companyModel.getPageSize()))
                //排序
                .withSort(SortBuilders.fieldSort("id").order(SortOrder.DESC))
                .build();
        SearchHits<CompanyModel> articleEntities = elasticsearchRestTemplate.search(nativeSearchQuery, CompanyModel.class);
        List<CompanyModel> list = new ArrayList<>();
        for(SearchHit<CompanyModel> searchHits:articleEntities.getSearchHits()){
            CompanyModel model = searchHits.getContent();
            list.add(model);
        }

        return ResultModel.getResultModel("",0,1,list);
    }

    @Override
    public ResultModel esFindByStr(CompanyModel companyModel) {
        HighlightBuilder.Field field = new HighlightBuilder.Field("address")
                .preTags("<font color='red'>")
                .postTags("</font>");
        NativeSearchQuery nativeSearchQuery = new NativeSearchQueryBuilder()
                .withQuery(matchQuery("address", companyModel.getAddress()))
                .withHighlightFields(field)
                .withPageable(PageRequest.of(companyModel.getPageNum()-1,companyModel.getPageSize()))
                //排序
                .withSort(SortBuilders.fieldSort("id").order(SortOrder.DESC))
                .build();
        SearchHits<CompanyModel> articleEntities = elasticsearchRestTemplate.search(nativeSearchQuery, CompanyModel.class);

        List<SearchHit<CompanyModel>> searchHitList = articleEntities.getSearchHits();
        List<CompanyModel> list = new ArrayList<>(searchHitList.size());
        for(SearchHit<CompanyModel> searchHits:searchHitList){
            CompanyModel model = searchHits.getContent();
            String highlightName = searchHits.getHighlightField("address").get(0);
            model.setAddress(highlightName);
            list.add(model);
        }

        return ResultModel.getResultModel("",0,1,list);
    }
}
